package com.xxc.service.impl;

import com.xxc.dao.ClazzDao;
import com.xxc.entity.Clazz;
import com.xxc.entity.Tag;
import com.xxc.fegin.TagClient;
import com.xxc.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiangcheng
 * @version 2022/6/21 17:34
 * @since JDK8
 */
@Service("clazzService")
@Transactional
public class ClazzServiceImpl implements ClazzService {
    private ClazzDao clazzDao;
    private TagClient tagClient;

    @Autowired
    public void setClazzDao(ClazzDao clazzDao) {
        this.clazzDao = clazzDao;
    }

    @Autowired
    public void setTagClient(TagClient tagClient) {
        this.tagClient = tagClient;
    }

    @Override
    public List<Clazz> queryAll() {
        List<Clazz> list = clazzDao.queryAll();
        list.forEach(clazz -> {
            Integer tagId = clazz.getTagId();
            Tag tag = tagClient.tag(tagId);
            clazz.setTag(tag);
        });
        return list;
    }

    @Override
    public Clazz insert(Clazz clazz) {
        this.clazzDao.insert(clazz);
        return clazz;
    }

    @Override
    public void deleteById(Integer id) {
        this.clazzDao.deleteById(id);
    }

    @Override
    public Clazz queryById(Integer id) {
        return clazzDao.queryById(id);
    }
}
